package com.invengo.module_check.ui.fragment.firstpos.fragment;

import static com.invengo.module_check.data.entity.BundleKey.KEY_COUNT;
import static com.invengo.module_check.data.entity.BundleKey.KEY_INDEX;
import static com.invengo.module_check.data.entity.BundleKey.KEY_SHELFS;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.databinding.FragmentFirstPositioningBinding;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;

public class FirstPosFragment extends BaseFragment<FragmentFirstPositioningBinding, FirstPosViewModel> {
    private int index;
    private int count;
    private ShelfBean bean;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_first_positioning;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initParam() {
        index = getArguments().getInt(KEY_INDEX);
        count = getArguments().getInt(KEY_COUNT);
        bean = (ShelfBean) getArguments().getSerializable(KEY_SHELFS);
    }

    @Override
    public void initData() {
        viewModel.initDefaultData(index, count, bean);
    }

    private MaterialDialog removeDialog;

    @Override
    public void initViewObservable() {
        Messenger.getDefault().register(this, getClass().getSimpleName() + getTag(), BookBean.class, bean -> {
            viewModel.updateNewBook(bean);
        });
        viewModel.ui.removeBookEvent.observe(this, bean -> {
            if (removeDialog == null)
                removeDialog = MaterialDialogUtils.showBasicDialog(getActivity(), "温馨提示!", "是否删除现定首书？")
                        .positiveText("确定")
                        .negativeText("取消")
                        .onPositive((dialog, which) -> {
                            viewModel.removeFirstBooks();
                        })
                        .onNegative((dialog, which) -> {

                        }).build();
            if (!removeDialog.isShowing())
                removeDialog.show();
        });
    }
}
